package cn.iocoder.yudao.module.forward.controller.admin.testtemplate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.testtemplate.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.testtemplate.TestTemplateDO;
import cn.iocoder.yudao.module.forward.service.testtemplate.TestTemplateService;

@Tag(name = "管理后台 - 演练模板")
@RestController
@RequestMapping("/forward/test-template")
@Validated
public class TestTemplateController {

    @Resource
    private TestTemplateService testTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建演练模板")
    @PreAuthorize("@ss.hasPermission('forward:test-template:create')")
    public CommonResult<Integer> createTestTemplate(@Valid @RequestBody TestTemplateSaveReqVO createReqVO) {
        return success(testTemplateService.createTestTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新演练模板")
    @PreAuthorize("@ss.hasPermission('forward:test-template:update')")
    public CommonResult<Boolean> updateTestTemplate(@Valid @RequestBody TestTemplateSaveReqVO updateReqVO) {
        testTemplateService.updateTestTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除演练模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:test-template:delete')")
    public CommonResult<Boolean> deleteTestTemplate(@RequestParam("id") Integer id) {
        testTemplateService.deleteTestTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得演练模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:test-template:query')")
    public CommonResult<TestTemplateRespVO> getTestTemplate(@RequestParam("id") Integer id) {
        TestTemplateDO testTemplate = testTemplateService.getTestTemplate(id);
        return success(BeanUtils.toBean(testTemplate, TestTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得演练模板分页")
    @PreAuthorize("@ss.hasPermission('forward:test-template:query')")
    public CommonResult<PageResult<TestTemplateRespVO>> getTestTemplatePage(@Valid TestTemplatePageReqVO pageReqVO) {
        PageResult<TestTemplateDO> pageResult = testTemplateService.getTestTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TestTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出演练模板 Excel")
    @PreAuthorize("@ss.hasPermission('forward:test-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTestTemplateExcel(@Valid TestTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TestTemplateDO> list = testTemplateService.getTestTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "演练模板.xls", "数据", TestTemplateRespVO.class,
                        BeanUtils.toBean(list, TestTemplateRespVO.class));
    }

}